package com.jee.mql.dao;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.Vector;

import com.mysql.jdbc.Field;
import com.mysql.jdbc.RowData;

public class Database {
	private DataSource ds;
	private Connection db;
	private DatabaseMetaData dbm;
	private Vector<Object> session = new Vector();
	
	public Database(){
			
	}
	
	/*test*/
	
	
	public Database(DataSource ds) {
		super();
		this.ds = ds;
		this.db = ds.getConnection();
	}



	public DataSource getDataSource(){
		return ds;
	}

	public void setDataSource(DataSource dataSource){
		this.ds = dataSource;
		db = dataSource.getConnection();
		try {
			dbm = db.getMetaData();
		} catch (Exception e) {
			System.out.println("Erreur : 2"+e.getMessage());
		}
	}
	
	public String [][] select(String tableName){
		String data[][] = null;
		try {
			String query = "SELECT * FROM "+tableName;
			Statement sql = db.createStatement();
			ResultSet rs = sql.executeQuery(query);
			ResultSetMetaData rsm = rs.getMetaData();
			int cols = rsm.getColumnCount();
			rs.last();
			int rows = rs.getRow();
			data = new String[rows][cols];
			rs.beforeFirst();
			
			int row = 0;
			
			while (rs.next()) {
				for (int col = 1; col <= cols; col++) {
					data[row][col-1] = rs.getString(col);
				}
				row++;
			}
			
			return data;
			
		} catch (Exception e) {
			System.out.println("Erreur : 3 "+e.getMessage());
			return null;
		}
	}
	
	public String []select(String tableName, String key, String value){
		String row[] = null;
		try {
			String query = "SELECT * FROM "+tableName+ "WHERE "+ key + " = '"+value+"'";
			Statement sql = db.createStatement();
			ResultSet rs = sql.executeQuery(query);
			ResultSetMetaData rsm = rs.getMetaData();
			int cols = rsm.getColumnCount();
			row = new String[cols];
			if (rs.next()) {
				for (int col = 1; col < cols; col++) {
					row[col-1] = rs.getString(col);					
				}
			}
			
			return row;
			
		} catch (Exception e) {
			System.out.println("Erreur : "+e.getMessage());
			return null;
		}
	}
	
	
	public int insert(String tableName, String row[]){
		
		try {
			if(row==null || row.length <=0) return -1;
			
			StringBuffer query = new StringBuffer("INSERT INTO "+tableName
					+" VALUES ('"+row[0]+"'");
			for (int i = 1; i < row.length; i++) {
				query.append(", '"+row[i] + "'");
			}
			
			query.append(")");
			
			Statement sql = db.createStatement();
			return sql.executeUpdate(query.toString());
			
		} catch (Exception e) {
			System.out.println("Erreur Insert : " + e.getMessage());
		}
		
		return 0;
	}
	
	public int insert(Object model){
	
		try {
			
			String tableName = model.getClass().getSimpleName();
			java.lang.reflect.Field[] f = model.getClass().getDeclaredFields();
			String row[] = new String[f.length];
			
			for (int i = 0; i < f.length; i++) {
				f[i].setAccessible(true);
				Object value = f[i].get(model);
				System.out.println(f[i].getName() + " : " + value);
				f[i].setAccessible(false);
				row[i] = "" + value;
			}
			
			int r = insert(tableName, row);
			if(r>0){
				session.add(model);
			}
			return r;
			
		} catch (Exception e) {
			System.out.println("Erreur : "+e.getMessage());
			return -1;
		}

	}	
	
	
	public int update(Object model){
		
			
			try {
				
				String tableName = model.getClass().getSimpleName();
				java.lang.reflect.Field[] f = model.getClass().getDeclaredFields();
				String row[] = new String[f.length];
				
				for (int i = 0; i < f.length; i++) {
					f[i].setAccessible(true);
					Object value = f[i].get(model);
					System.out.println(f[i].getName() + " : " + value);
					f[i].setAccessible(false);
					row[i] = "" + value;
				}
				
				int r = insert(tableName, row);
				if(r>0){
					session.add(model);
				}
				return r;
				
			} catch (Exception e) {
				System.out.println("Erreur : "+e.getMessage());
				return -1;
			}
		
		
	}
	
	void close(){
		for (Object x : session) {
			update(x);
		}
	}
}















